package info.jimao.newsdk.results;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import info.jimao.newsdk.utilities.DateDeserializer;

/**
 * 列表查询结果
 *
 * @param <T> 返回的数据列表datas中的数据类型
 */
public class JuHeListDataResult<T> implements Serializable {
    private static final long serialVersionUID = -2576439904292678787L;

    protected boolean success;
    protected String message;
    protected String errorCode;

    private String reason;
    private Serializable error_code;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    protected String accessToken;

    private List<T> datas;
    Class<T> type;

    public List<T> getDatas() {
        return datas;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
    }

    public JuHeListDataResult(boolean success, String message) {
        super();
        this.success = success;
        this.message = message;
        this.datas = new ArrayList<T>();
    }

    public JuHeListDataResult(String jsonString, Class<T> type) {
        this(jsonString, type, new DateDeserializer());
    }

    public JuHeListDataResult(String jsonString, Class<T> type, JsonDeserializer<Date> dateDeserializer) {
        this.type = type;
        try {
            JSONObject root = new JSONObject(jsonString);

            removeQuote(jsonString);

            this.success = !root.isNull("Success") && root.getBoolean("Success");
            this.message = !root.isNull("Message") ? root.getString("Message") : "";
            this.errorCode = !root.isNull("ErrorCode") ? root.getString("ErrorCode") : "";
            this.accessToken = !root.isNull("AccessToken") ? root.getString("AccessToken") : "";

            setError_code(!root.isNull("error_code") ? root.getInt("error_code") : "");
            setReason(!root.isNull("reason") ? root.getString("reason") : "");

            Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, dateDeserializer).create();

            JSONArray jsonDatas;

            if (!root.isNull("result")) {
                jsonDatas = root.getJSONArray("result");


            } else {
                jsonDatas = new JSONArray() {
                };
            }

            List<T> datas = new ArrayList<T>();

            for (int i = 0; i < jsonDatas.length(); i++) {
                JSONObject jsonData = jsonDatas.getJSONObject(i);
                T t = gson.fromJson(jsonData.toString(), this.type);
                datas.add(t);
            }

            setDatas(datas);

        } catch (Exception e) {
            this.success = false;
            this.message = "数据转换异常";
        }
    }

    public Serializable getError_code() {
        return error_code;
    }

    public void setError_code(Serializable error_code) {
        this.error_code = error_code;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    private String removeQuote(String jsonString) {
        String regex = "\"title\":\"[^:\"]+(\"[^:\"]+\")[^\"]+\",\"date\":";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(jsonString);
        while (m.find()) {
            System.out.println(m.group());
        }

        return jsonString;
    }
}
